package base.service.impl;


import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import base.dao.ClassinfoDao;
import base.dao.GradeDao;
import base.dao.StudentDao;
import base.model.Classinfo;
import base.model.Grade;
import base.model.Student;
import base.service.GradeService;
import common.enums.ResultCodeEnum;
import common.exception.ServiceException;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


/**
 * 
 * @author xiaozewen
 * @version  2016-11-26 11:52:03
 */
@Service
public class GradeServiceImpl  extends BaseServiceImpl<Grade> implements GradeService {
	@Autowired
    private GradeDao gradeDao;
	@Autowired
	private StudentDao studentDao;
	@Autowired
	private ClassinfoDao classinfoDao;
	
	
	@Override
	public int save(Grade entity) throws ServiceException {
		Student searchS=new Student();
		searchS.setName(entity.getStudentName());
		Student student=studentDao.selectOne(searchS);
		if(student == null){
			throw new ServiceException(ResultCodeEnum.SERVICE_ERROR.getValue(), "不存在此学生");
		}
		Classinfo searchC=new Classinfo();
		searchC.setName(entity.getClassName());
		Classinfo classinfo=classinfoDao.selectOne(searchC);
		if(classinfo == null){
			throw new ServiceException(ResultCodeEnum.SERVICE_ERROR.getValue(), "不存在此课程");
		}
		entity.setStudentId(student.getId());
		entity.setClassId(classinfo.getId());
		return super.save(entity);
	}

	@Override
	public int delete(String ids) {
		Example example = new Example(Grade.class);
		example.createCriteria().andIn("id", Arrays.asList(ids.split(",")));
		return gradeDao.deleteByExample(example);
	}

	@Override
	public List<Grade> list(Grade grade) {
		Example example=new Example(Grade.class);
		Criteria criteria = example.createCriteria();
		List<Grade> grades = gradeDao.selectByExample(example);
		return grades;
	}

	@Override
	public PageInfo<Grade> listPage(Grade grade,Integer pageNum,Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Example example=new Example(Grade.class);
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(grade.getStudentName())){
			//criteria.andLike("studentName",  "%%"+grade.getStudentName()+"%%");
			criteria.andCondition("CONCAT(student_name,pinyin(student_name)) like '%"+grade.getStudentName()+"%'");
		}
		List<Grade> grades = gradeDao.selectByExample(example);
		return new PageInfo<Grade>(grades);
	}

	@Override
	public void update(Grade grade) {
		Student searchS=new Student();
		searchS.setName(grade.getStudentName());
		Student student=studentDao.selectOne(searchS);
		if(student == null){
			throw new ServiceException(ResultCodeEnum.SERVICE_ERROR.getValue(), "不存在此学生");
		}
		Classinfo searchC=new Classinfo();
		searchC.setName(grade.getClassName());
		Classinfo classinfo=classinfoDao.selectOne(searchC);
		if(classinfo == null){
			throw new ServiceException(ResultCodeEnum.SERVICE_ERROR.getValue(), "不存在此课程");
		}
		grade.setStudentId(student.getId());
		grade.setClassId(classinfo.getId());
		gradeDao.updateByPrimaryKey(grade);
	}

}
